package com.shoy.myapp.base;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import com.shoy.myapp.app.AppContext;
import com.shoy.myapp.app.AppManager;

/**
 * Activity 基础类
 *
 * @author shay
 * @created 2015/3/26
 */
public abstract class BaseActivity extends FragmentActivity implements IBaseActivity {
    // 是否允许销毁
    private boolean allowDestroy = true;
    private View destroyView;
    private Operation mOperation = null;
    private View mContextView;
    protected final String TAG = this.getClass().getSimpleName();
    protected AppContext mApplication;

    protected void setAllowDestroy(boolean allowDestroy) {
        this.allowDestroy = allowDestroy;
    }

    protected void setAllowDestroy(boolean allowDestroy, View destroyView) {
        this.allowDestroy = allowDestroy;
        this.destroyView = destroyView;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getInstance().add(this);
        mOperation = new Operation(this);
        mApplication = (AppContext) this.getApplicationContext();
        //渲染视图
        mContextView = LayoutInflater.from(this).inflate(bindLayout(), null);
        setContentView(mContextView);

        //初始化控件
        initView(mContextView);

        //初始化业务逻辑
        doBusiness(this);
    }

    @Override
    public void doBusiness(Context context) {
        business(context);
    }

    protected void business(Context context) {

    }

    /**
     * 获取共通操作机能
     */
    public Operation getOperation() {
        return this.mOperation;
    }

    @Override
    protected void onPause() {
        super.onPause();
//        MobclickAgent.onPageEnd(TAG);
//        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        MobclickAgent.onPageStart(TAG);
//        MobclickAgent.onResume(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getInstance().finish(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && destroyView != null) {
            destroyView.onKeyDown(keyCode, event);
            if (!allowDestroy) {
                return false;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
